
class StoreController < ApplicationController
  layout "sidebar"
  
  # Main page of the store, shows promoted products
  def index
    @categories = Category.order("name")
    @cart = current_cart
    
    @products = Product.order("RANDOM()").find(:all, :conditions => [ "promote = ?", true], :limit => 5)
    
  end
  
  # Shows all products of a category
  # GET /store/category/1
  def category
    @categories = Category.order("name")
    @category = Category.find(params[:id])
    @products = @category.product.all;
    @cart = current_cart

    respond_to do |format|
      format.html # category.html.erb
    end
  end
  
  # Shows a single product page
  # GET /store/product/1
  def product
    @categories = Category.order("name")
    @product = Product.find(params[:id]);
    @cart = current_cart
    
    
    respond_to do |format|
      format.html # category.html.erb
      
      format.pdf do
        pdf = ProductPdf.new(@product, view_context)
        send_data pdf.render, filename:
        "product_#{@product.id}.pdf",
        type: "application/pdf"
      end
      
    end
  end
end
